package com.wing.bill.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.bill.dao.WarehouseProcessBillDetailDao;
import com.wing.bill.model.entity.BillDetailInfo;
import com.wing.bill.model.entity.WarehouseBillDetail;
import com.wing.bill.model.entity.WarehouseProcessBillDetail;
import com.wing.bill.model.enums.BillStatusEnum;
import com.wing.bill.model.request.WarehouseBillDetailForm;
import com.wing.bill.model.request.WarehouseProcessBillDetailForm;
import com.wing.bill.model.response.WarehouseBillDetailVO;
import com.wing.bill.model.response.WarehouseProcessBillDetailVO;
import com.wing.common.utils.DateUtils;
import com.wing.common.utils.PageResult;
import com.wing.product.dao.WarehouseProductionOrderDao;
import com.wing.product.dao.WarehouseProductionOrderDetailDao;
import com.wing.product.model.entity.WarehouseProductionOrderDetail;
import com.wing.product.model.response.WarehouseProductionOrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class WarehouseProcessBillDetailServiceImpl extends ServiceImpl<WarehouseProcessBillDetailDao, WarehouseProcessBillDetail> implements WarehouseProcessBillDetailService{

    @Autowired
    private WarehouseProductionOrderDao warehouseProductionOrderDao;

    @Autowired
    WarehouseProductionOrderDetailDao warehouseProductionOrderDetailDao;

    @Override
    public boolean save(WarehouseProcessBillDetailForm warehouseBillDetailForm) {
        WarehouseProcessBillDetail warehouseBillDetail = new WarehouseProcessBillDetail();
        warehouseBillDetail.setCustomerId(warehouseBillDetailForm.getCustomerId());
        warehouseBillDetail.setSysBundleCode(warehouseBillDetailForm.getSysBundleCode());
        warehouseBillDetail.setBillPeriod(DateUtils.getYearMonth());
        warehouseBillDetail.setStartDate(LocalDateTime.now());
        warehouseBillDetail.setBillStatus(BillStatusEnum.UNPAID.getCode());
        warehouseBillDetail.setBillPrice(warehouseBillDetailForm.getBillPrice());
        return this.save(warehouseBillDetail);
    }

    @Override
    public Boolean update(WarehouseProcessBillDetailForm warehouseBillDetailForm){
        WarehouseProcessBillDetail warehouseBillDetail = this.getById(warehouseBillDetailForm.getId());
        warehouseBillDetail.setBillStatus(BillStatusEnum.PAID.getCode());
        return this.updateById(warehouseBillDetail);
    }

    @Override
    public PageResult<Page<WarehouseProcessBillDetailVO>> pageList4app(Page<WarehouseProcessBillDetail> pg, Map paramMap) {
        return new PageResult<>(baseMapper.list4app(pg, paramMap));
    }

    @Override
    public Boolean updateBillDetail(Long customerId, List<String> sysBundleCodeList){
        // 查询本月账单
        List<WarehouseProcessBillDetail> warehouseBillDetailList = baseMapper
                .selectList(
                        new LambdaQueryWrapper<WarehouseProcessBillDetail>()
                                .eq(WarehouseProcessBillDetail::getCustomerId, customerId)
                                .in(WarehouseProcessBillDetail::getSysBundleCode,sysBundleCodeList)
                                .eq(WarehouseProcessBillDetail::getBillPeriod, DateUtils.getYearMonth())
                                .ne(WarehouseProcessBillDetail::getTransferType,3));
        warehouseBillDetailList.forEach(warehouseBillDetail -> {
            warehouseBillDetail.setEndDate(LocalDateTime.now());
        });
        return updateBatchById(warehouseBillDetailList);
    }

    @Override
    public Boolean addBillDetail(Long customerId, List<BillDetailInfo> sysBundleCodeList , Long billId){
        List<WarehouseProcessBillDetail> warehouseBillDetailList = sysBundleCodeList.stream().map(billDetailInfo -> {
            WarehouseProcessBillDetail warehouseBillDetail = new WarehouseProcessBillDetail();
            warehouseBillDetail.setCustomerId(customerId);
            warehouseBillDetail.setWarehouseBillId(billId);
            warehouseBillDetail.setSysBundleCode(billDetailInfo.getSysBundleCode());
            warehouseBillDetail.setTransferType(billDetailInfo.getTransferType());
            warehouseBillDetail.setBillPrice(billDetailInfo.getPrice());
            warehouseBillDetail.setBillPeriod(DateUtils.getYearMonth());
            //修改为交割日期
            if(null!=billDetailInfo.getHandoverTime()){
                warehouseBillDetail.setStartDate(billDetailInfo.getHandoverTime());
            }else{
                warehouseBillDetail.setStartDate(LocalDateTime.now());
            }
            warehouseBillDetail.setBillStatus(BillStatusEnum.UNPAID.getCode());
            return warehouseBillDetail;
        }).collect(Collectors.toList());
        return saveBatch(warehouseBillDetailList);
    }

    @Override
    public Boolean addBillDetailOrder(Long customerId, Long billId,Double totalProcessingFee,Long warehouseOrderId){
        WarehouseProcessBillDetail warehouseBillDetail = new WarehouseProcessBillDetail();
        warehouseBillDetail.setCustomerId(customerId);
        warehouseBillDetail.setWarehouseBillId(billId);
        warehouseBillDetail.setTransferType(3);
        warehouseBillDetail.setBillPrice(totalProcessingFee);
        warehouseBillDetail.setBillPeriod(DateUtils.getYearMonth());
        warehouseBillDetail.setStartDate(LocalDateTime.now());
        warehouseBillDetail.setWarehouseOrderId(warehouseOrderId);
        return save(warehouseBillDetail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateAllBillDetail() {
        List<WarehouseProcessBillDetail> warehouseBillDetailList = baseMapper
                .selectList(
                        new LambdaQueryWrapper<WarehouseProcessBillDetail>()
                                .eq(WarehouseProcessBillDetail::getBillPeriod, DateUtils.getLastMonth()));
        warehouseBillDetailList.forEach(warehouseBillDetail -> {
            warehouseBillDetail.setEndDate(LocalDateTime.now());
        });
        System.out.println("============"+DateUtils.getYearMonth());
        System.out.println("============"+DateUtils.getLastMonth());
        return updateBatchById(warehouseBillDetailList);
    }

    @Override
    public Boolean updateBillDetailByBillId(Long id) {
        LambdaUpdateWrapper<WarehouseProcessBillDetail> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(WarehouseProcessBillDetail::getBillStatus, BillStatusEnum.PAID.getCode())
                .eq(WarehouseProcessBillDetail::getWarehouseBillId, id);
        return this.update(null, updateWrapper);
    }

    @Override
    public WarehouseProcessBillDetailVO selectBillDetail(String id){

        WarehouseProcessBillDetailVO warehouseProcessBillDetailVO = this.baseMapper.selectProcessDetailOne(id);

        WarehouseProductionOrderVO warehouseProductionOrderVO = warehouseProductionOrderDao.selectProductionDetail(warehouseProcessBillDetailVO.getWarehouseOrderId().toString());
        //查询生产记录明细
        LambdaQueryWrapper<WarehouseProductionOrderDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WarehouseProductionOrderDetail::getWarehouseProductionOrderId, warehouseProductionOrderVO.getId())
                .eq(WarehouseProductionOrderDetail::getIsDeleted, 0);
        List<WarehouseProductionOrderDetail> warehouseProductionOrderDetailList = warehouseProductionOrderDetailDao.selectList(lambdaQueryWrapper);
        warehouseProcessBillDetailVO.setWarehouseProductionOrderDetailList(warehouseProductionOrderDetailList);
        return warehouseProcessBillDetailVO;
    }
}
